package com.google.code.swingonfire.showcase.graphic.sketch;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

import com.google.code.swingonfire.image.ComponentBasedImageProvider;
import com.google.code.swingonfire.plotter.ArrowSketch;
import com.google.code.swingonfire.plotter.Plotter;
import com.google.code.swingonfire.utility.Colors;

public class PlotterComponent extends JComponent
{

    private static final long serialVersionUID = -3008186636375983221L;

    private final ComponentBasedImageProvider imageProvider = new ComponentBasedImageProvider(this, false)
    {

        @Override
        protected void drawImage(Graphics2D g, int width, int height)
        {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, width, height);

            if (plotter != null)
            {
                plotter.size(new Dimension((int) (width * getScale()), (int) (height * getScale()))).rotate(
                    getRotation());

                g.setColor(Colors.translucent(Color.BLUE, 0.1));
                plotter.drawBounds(g, width / 2, height / 2);

                g.setColor(Color.orange);
                plotter.draw(g, width / 2, height / 2);
            }
        }
    };

    private Plotter plotter = new Plotter(new ArrowSketch(0.5, 0.5, 0.5));
    private double scale = 0.5;
    private double rotation = 0.0;

    public PlotterComponent()
    {
        super();
    }

    public Plotter getPlotter()
    {
        return plotter;
    }

    public void setPlotter(Plotter plotter)
    {
        this.plotter = plotter;

        imageProvider.updateNeeded();
    }

    public double getScale()
    {
        return scale;
    }

    public void setScale(double scale)
    {
        this.scale = scale;
        imageProvider.updateNeeded();
    }

    public double getRotation()
    {
        return rotation;
    }

    public void setRotation(double rotation)
    {
        this.rotation = rotation;
        imageProvider.updateNeeded();
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);

        g.drawImage(imageProvider.getImage(), 0, 0, null);
    }

}
